﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using WFNetLib;

namespace 无线打分网站.webAPI
{
    public partial class API_Submit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            JSON_SubmitData SD = new JSON_SubmitData();
            try
            {
                string uid, eid, type, ZuoHao, grade;
                uid = Request.QueryString["uid"];
                eid = Request.QueryString["eid"];
                type = Request.QueryString["type"];
                ZuoHao = Request.QueryString["ZuoHao"];
                grade = Request.QueryString["grade"];
                UserExData ue = UserExDataDBOption.GetUserExData(new Guid(uid));
                if (ue == null)
                {
                    SD.errIndex = 4;
                    SD.message = "缺少帐户信息";
                    return;
                }
                ExperimentData ed = ExperimentDataDBOption.GetExperimentData(new Guid(eid));
                if (ed == null)
                {
                    SD.errIndex = 5;
                    SD.message = "缺少实验信息";
                    return;
                }
                switch (DateTime.Now.DayOfWeek)
                {
                    case DayOfWeek.Monday:
                        if(ed.Week1==false)
                        {
                            SD.errIndex = 6;
                            SD.message = "当天没有课";
                            return;
                        }
                        break;
                    case DayOfWeek.Tuesday:
                        if (ed.Week2 == false)
                        {
                            SD.errIndex = 6;
                            SD.message = "当天没有课";
                            return;
                        }
                        break;
                    case DayOfWeek.Wednesday:
                        if (ed.Week3 == false)
                        {
                            SD.errIndex = 6;
                            SD.message = "当天没有课";
                        return;
                        }
                        break;
                    case DayOfWeek.Thursday:
                        if (ed.Week4 == false)
                        {
                            SD.errIndex = 6;
                            SD.message = "当天没有课";
                            return;
                        }
                        break;
                    case DayOfWeek.Friday:
                        if (ed.Week5 == false)
                        {
                            SD.errIndex = 6;
                            SD.message = "当天没有课";
                        return;
                        }
                        break;
                    case DayOfWeek.Saturday:
                        if (ed.Week6 == false)
                        {
                            SD.errIndex = 6;
                            SD.message = "当天没有课";
                            return;
                        }
                        break;
                    case DayOfWeek.Sunday:
                        if (ed.Week7 == false)
                        {
                            SD.errIndex = 6;
                            SD.message = "当天没有课";
                            return;
                        }
                        break;
                }
                JSON_ExperimentTime[] Times = JsonSerializeHelper.DeSerialize<JSON_ExperimentTime[]>(ed.Times);
                bool bok = false;
                int time = DateTime.Now.Hour * 100 + DateTime.Now.Minute;
                JSON_ExperimentTime tok=new JSON_ExperimentTime();
                foreach (JSON_ExperimentTime t in Times)
                {                    
                    if (time < t.EndTime && time > t.StartTime)
                    {
                        bok = true;
                        tok = t;
                        break;
                    }
                }
                if (bok == false)
                {
                    SD.errIndex = 7;
                    SD.message = "当天时间没有课";
                    return;
                }
                if (type == "1")//教师评价
                {
                    if (TeachGradeDataDBOption.CheckResubmit(ed.Name, int.Parse(ZuoHao), tok.StartTime, tok.EndTime))
                    {
                        SD.errIndex = 2;
                        SD.message = "重复提交";
                        return;
                    }
                    TeachGradeData tgd = new TeachGradeData();
                    tgd.Datetime = DateTime.Now;
                    tgd.Grade = int.Parse(grade);
                    tgd.Room = ed.Room;
                    tgd.ShiYan = ed.Name;
                    tgd.Teacher = ue.Name;
                    tgd.ZuoHao = int.Parse(ZuoHao);
                    TeachGradeDataDBOption.Insert(tgd);
                    SD.success = true;
                    SD.errIndex = 1;
                    SD.message = "成功提交";
                    return;
                }
                else if (type == "2")//实验评价
                {
                    if (ExperimentGradeDataDBOption.CheckResubmit(ed.Name, int.Parse(ZuoHao), tok.StartTime, tok.EndTime))
                    {
                        SD.errIndex = 2;
                        SD.message = "重复提交";
                        return;
                    }
                    ExperimentGradeData egd = new ExperimentGradeData();
                    egd.Datetime = DateTime.Now;
                    egd.Grade = int.Parse(grade);
                    egd.Room = ed.Room;
                    egd.ShiYan = ed.Name;
                    egd.Teacher = ue.Name;
                    egd.ZuoHao = int.Parse(ZuoHao);
                    ExperimentGradeDataDBOption.Insert(egd);
                    SD.success = true;
                    SD.errIndex = 1;
                    SD.message = "成功提交";
                    return;
                }
            }
            catch(Exception ex)
            {
                SD.errIndex = 3;
                SD.message = "未知错误:"+ex.Message;
                return;
            }
            finally
            {
                Response.Write(JsonSerializeHelper.Serialize(SD));//失败
            }
        }
    }
}